﻿using GKQC.Common;
using System;using GKQC.Language;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Machine
{
    /// <summary>
    /// 自动测试仪Z/计算
    /// </summary>
    public class OffsetCompute1601
    {
        Offset1601 _Offset;
        readonly string FILENAME = System.Environment.CurrentDirectory + "\\Settings\\Offset1601.dat";
        public OffsetCompute1601()
        {
            try
            {
                _Offset = Settings.GetDataObjectFile<Offset1601>(FILENAME);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }

            if(_Offset == null)
            {
                _Offset = new Offset1601();
            }
        }

        public void AddOffsetEntity(string freq, OffsetEntity1601 offfsetEntity1601)
        {
            if (offfsetEntity1601 == null) return;
            if (_Offset.Offsets.ContainsKey(freq)) return;
            _Offset.Offsets.Add(freq, offfsetEntity1601);

        }
        public void SetOffsetEntity(string freq, int channel, decimal Rs, decimal Xs)
        {
            OffsetEntity1601 offsetEntity = null;
            if (_Offset.Offsets.ContainsKey(freq))
                offsetEntity = _Offset.Offsets[freq];
            if (offsetEntity == null)
            {
                offsetEntity = new OffsetEntity1601(freq);
                _Offset.Offsets[freq] = offsetEntity;
            }

            switch (channel)
            {
                #region
                case 1:
                    offsetEntity.Rs1 = Rs;
                    offsetEntity.Xs1 = Xs;
                    break;
                case 2:
                    offsetEntity.Rs2 = Rs;
                    offsetEntity.Xs2 = Xs;
                    break;
                case 3:
                    offsetEntity.Rs3 = Rs;
                    offsetEntity.Xs3 = Xs;
                    break;
                case 4:
                    offsetEntity.Rs4 = Rs;
                    offsetEntity.Xs4 = Xs;
                    break;
                case 5:
                    offsetEntity.Rs5 = Rs;
                    offsetEntity.Xs5 = Xs;
                    break;
                case 6:
                    offsetEntity.Rs6 = Rs;
                    offsetEntity.Xs6 = Xs;
                    break;
                case 7:
                    offsetEntity.Rs7 = Rs;
                    offsetEntity.Xs7 = Xs;
                    break;
                case 8:
                    offsetEntity.Rs8 = Rs;
                    offsetEntity.Xs8 = Xs;
                    break;
                case 9:
                    offsetEntity.Rs9 = Rs;
                    offsetEntity.Xs9 = Xs;
                    break;
                case 10:
                    offsetEntity.Rs10 = Rs;
                    offsetEntity.Xs10 = Xs;
                    break;
                #endregion
                #region
                case 11:
                    offsetEntity.Rs11 = Rs;
                    offsetEntity.Xs11 = Xs;
                    break;
                case 12:
                    offsetEntity.Rs12 = Rs;
                    offsetEntity.Xs12 = Xs;
                    break;
                case 13:
                    offsetEntity.Rs13 = Rs;
                    offsetEntity.Xs13 = Xs;
                    break;
                case 14:
                    offsetEntity.Rs14 = Rs;
                    offsetEntity.Xs14 = Xs;
                    break;
                case 15:
                    offsetEntity.Rs15 = Rs;
                    offsetEntity.Xs15 = Xs;
                    break;
                case 16:
                    offsetEntity.Rs16 = Rs;
                    offsetEntity.Xs16 = Xs;
                    break;
                case 17:
                    offsetEntity.Rs17 = Rs;
                    offsetEntity.Xs17 = Xs;
                    break;
                case 18:
                    offsetEntity.Rs18 = Rs;
                    offsetEntity.Xs18 = Xs;
                    break;
                case 19:
                    offsetEntity.Rs19 = Rs;
                    offsetEntity.Xs19 = Xs;
                    break;
                case 20:
                    offsetEntity.Rs20 = Rs;
                    offsetEntity.Xs20 = Xs;
                    break;
                #endregion

                default:
                    break;
            }
        }

        public Dictionary<string,OffsetEntity1601> GetOffsets()
        {
            return _Offset.Offsets;
        }

        public OffsetEntity1601 GetOffsetEntity(string freq)
        {
            if (_Offset.Offsets.ContainsKey(freq))
                return _Offset.Offsets[freq];
            else
                return null;
        }

        public void SaveOffset()
        {
            try
            {
                Settings.SaveObject(_Offset, FILENAME);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
                throw ex;
            }
        }

        public void ComputeZR(decimal Rt, decimal Xt, string freq, int channel, out decimal Rc, out decimal Zc, out decimal Xc)
        {
            Rc = Rt;
            Xc = Xt;
            Zc = 0;
            decimal Rs, Xs;
            Rs = Xs = 0;

            if (_Offset == null) return;
            if(_Offset.Offsets == null) return;
            var offset = _Offset.Offsets[freq];
            if (offset != null)
            {
                switch (channel)
                {
                    #region
                    case 1:
                        Rs = offset.Rs1;
                        Xs = offset.Xs1;
                        break;
                    case 2:
                        Rs = offset.Rs2;
                        Xs = offset.Xs2;
                        break;
                    case 3:
                        Rs = offset.Rs3;
                        Xs = offset.Xs3;
                        break;
                    case 4:
                        Rs = offset.Rs4;
                        Xs = offset.Xs4;
                        break;
                    case 5:
                        Rs = offset.Rs5;
                        Xs = offset.Xs5;
                        break;
                    case 6:
                        Rs = offset.Rs6;
                        Xs = offset.Xs6;
                        break;
                    case 7:
                        Rs = offset.Rs7;
                        Xs = offset.Xs7;
                        break;
                    case 8:
                        Rs = offset.Rs8;
                        Xs = offset.Xs8;
                        break;
                    case 9:
                        Rs = offset.Rs9;
                        Xs = offset.Xs9;
                        break;
                    case 10:
                        Rs = offset.Rs10;
                        Xs = offset.Xs10;
                        break;
                    #endregion
                    #region
                    case 11:
                        Rs = offset.Rs11;
                        Xs = offset.Xs11;
                        break;
                    case 12:
                        Rs = offset.Rs12;
                        Xs = offset.Xs12;
                        break;
                    case 13:
                        Rs = offset.Rs13;
                        Xs = offset.Xs13;
                        break;
                    case 14:
                        Rs = offset.Rs14;
                        Xs = offset.Xs14;
                        break;
                    case 15:
                        Rs = offset.Rs15;
                        Xs = offset.Xs15;
                        break;
                    case 16:
                        Rs = offset.Rs16;
                        Xs = offset.Xs16;
                        break;
                    case 17:
                        Rs = offset.Rs17;
                        Xs = offset.Xs17;
                        break;
                    case 18:
                        Rs = offset.Rs18;
                        Xs = offset.Xs18;
                        break;
                    case 19:
                        Rs = offset.Rs19;
                        Xs = offset.Xs19;
                        break;
                    case 20:
                        Rs = offset.Rs20;
                        Xs = offset.Xs20;
                        break;
                    #endregion
                    default:
                        break;
                }
            }

            Rc = Rt - Rs;
            Xc = Xt - Xs;

            Zc = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble(Rc * Rc + Xc * Xc)));
        }
        public void SetTestTimes(int testTimes)
        {
            _Offset.TestTimes = testTimes;
        }
        public void SetLcFirstDelay(int lcFirstDelay)
        {
            _Offset.LcFirstDelay = lcFirstDelay;
        }
        public void SetRxTestTimes(int rxTestTimes)
        {
            _Offset.RxTestTimes = rxTestTimes;
        }
        public void SetLcrFirstDelay(int lcrFirstDealy)
        {
            _Offset.LcrFirstDealy = lcrFirstDealy;
        }

        public void SetChannelCount(int channelCount)
        {
            _Offset.ChannelCount = channelCount;
        }

        public int GetTestTimes()
        {
            return _Offset.TestTimes;
        }
        public int GetLcFirstDelay()
        {
            return _Offset.LcFirstDelay;
        }
        public int GetRxTestTimes()
        {
            return _Offset.RxTestTimes;
        }
        public int GetLcrFirstDelay()
        {
            return _Offset.LcrFirstDealy;
        }

        public double GetZOffset()
        {
            return _Offset.ZOffset;
        }
        public double GetROffset()
        {
            return _Offset.ROffset;
        }

        public void SetZOffset(double zOffset)
        {
            _Offset.ZOffset = zOffset;
        }
        public void SetROffset(double rOffset)
        {
            _Offset.ROffset = rOffset;
        }

        public int GetChannelCount()
        {
            if (_Offset.ChannelCount > 0) return _Offset.ChannelCount;
            else return 10;
        }

        public void SetBox20Time(decimal value)
        {
            _Offset.Box20Time = value;
        }

        public decimal GetBox20Time()
        {
            return _Offset.Box20Time;
        }


        #region 通道禁用/启用
        public bool Enable1 { get; set; }
        public bool Enable2 { get; set; }
        public bool Enable3 { get; set; }
        public bool Enable4 { get; set; }
        public bool Enable5 { get; set; }
        public bool Enable6 { get; set; }
        public bool Enable7 { get; set; }
        public bool Enable8 { get; set; }
        public bool Enable9 { get; set; }
        public bool Enable10 { get; set; }
        public bool Enable11 { get; set; }
        public bool Enable12 { get; set; }
        public bool Enable13 { get; set; }
        public bool Enable14 { get; set; }
        public bool Enable15 { get; set; }
        public bool Enable16 { get; set; }
        public bool Enable17 { get; set; }
        public bool Enable18 { get; set; }
        public bool Enable19 { get; set; }
        public bool Enable20 { get; set; }
        #endregion 通道禁用/启用
    }
}
